package com.hckj.jianzhijia.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.avos.avoscloud.AVObject;
import com.hckj.jianzhijia.R;
import com.hckj.jianzhijia.controllers.JobListController;
import com.hckj.jianzhijia.utils.DateUtils;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015-07-08.
 */
public class JobListAdapter extends BaseAdapter {


    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<AVObject> list;
    private JobListController jobListController;

    public JobListAdapter(Context context, ArrayList<AVObject> list) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.list = list;
        jobListController = new JobListController(context);
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        AVObject avObject = list.get(position);
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.item_job_list, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tv_job_name.setText(avObject.getString("jobName"));
        viewHolder.imgv_job_pic.setImageDrawable(jobListController.getImageResourceDrawable(avObject.getInt("jobType")));
        viewHolder.tv_publish_state.setText(jobListController.getJobStateString(avObject.getInt("jobType")));
        viewHolder.tv_publish_time.setText(DateUtils.getDateStringFromDate(avObject.getDate("createdAt")));
        return convertView;
    }

    private class ViewHolder {

        public ImageView imgv_job_pic;
        public TextView tv_job_name;
        public TextView tv_publish_time;
        public TextView tv_publish_state;

        public ViewHolder(View view){
            imgv_job_pic = (ImageView)view.findViewById(R.id.imgv_user_pic);
            tv_job_name = (TextView) view.findViewById(R.id.tv_user_name);
            tv_publish_time = (TextView) view.findViewById(R.id.tv_publish_time);
            tv_publish_state = (TextView) view.findViewById(R.id.tv_publish_state);
        }
    }
}

